//
//  FirstViewController.m
//  ybzj
//
//  Created by liyuan on 15/6/25.
//  Copyright (c) 2015年 liyuan. All rights reserved.
//

#import "FirstViewController.h"
#import "ClinicTableViewCell.h"
#import "ClinicDetailController.h"
#import "PushSegue.h"

@interface FirstViewController ()

@property (weak, nonatomic) IBOutlet UISearchBar *searchbar;
@property (weak, nonatomic) IBOutlet UIView * SearchView;
@property (strong, nonatomic) IBOutlet UITableView *ClicnicTable;
@property (weak, nonatomic) IBOutlet UITabBarItem *SearchBarItem;
@property (weak, nonatomic) IBOutlet UIView *SearchEntityView;
@property (weak, nonatomic) IBOutlet UIButton *SearchButton;
@property (strong,nonatomic) ClinicSearchViewController *cs;
@property (nonatomic)NSString *SearchContent;
@property (strong,nonatomic) ClinicDetailController *detail;






@end

@implementation FirstViewController

bool isSearching=NO;



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    CGFloat width=[UIScreen mainScreen].bounds.size.width;
    self.SearchView.backgroundColor=[UIColor colorWithRed:246/255.0 green:246/255.0 blue:247/255.0 alpha:1];
    _searchbar.hidden=YES;
    //set constrain for search entity view
    _SearchEntityView.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *TopConstrainforSearchEntityView=[NSLayoutConstraint constraintWithItem:_SearchEntityView
                                                                                    attribute:NSLayoutAttributeTop
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:_searchbar attribute:NSLayoutAttributeBottom
                                                                                   multiplier:1.0f
                                                                                     constant:-0];
    
    NSLayoutConstraint *BottomConstrainforSearchEntityView=[NSLayoutConstraint constraintWithItem:_SearchEntityView
                                                                                        attribute:NSLayoutAttributeBottom
                                                                                        relatedBy:NSLayoutRelationEqual
                                                                                           toItem:_SearchView attribute:NSLayoutAttributeBottom
                                                                                       multiplier:1.0f
                                                                                         constant:0];
    NSLayoutConstraint *leftConstrainforSearchEntityView=[NSLayoutConstraint constraintWithItem:_SearchEntityView
                                                                                        attribute:NSLayoutAttributeLeading
                                                                                        relatedBy:NSLayoutRelationEqual
                                                                                           toItem:_SearchView
                                                                                      attribute:NSLayoutAttributeLeading
                                                                                       multiplier:1.0f
                                                                                         constant:0];
    
    NSLayoutConstraint *rightConstrainforSearchEntityView=[NSLayoutConstraint constraintWithItem:_SearchEntityView
                                                                                      attribute:NSLayoutAttributeRight
                                                                                      relatedBy:NSLayoutRelationEqual
                                                                                         toItem:_SearchView
                                                                                       attribute:NSLayoutAttributeTrailing
                                                                                     multiplier:1.0f
                                                                                       constant:0];
    
    TopConstrainforSearchEntityView.active=YES;
    leftConstrainforSearchEntityView.active=YES;
    BottomConstrainforSearchEntityView.active=YES;
    rightConstrainforSearchEntityView.active=YES;
    _SearchEntityView.backgroundColor=[UIColor redColor];
    
    //set constrain for search button
    _SearchButton.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *TopConstrainforSearchButton=[NSLayoutConstraint constraintWithItem:_SearchButton
                                                                                 attribute:NSLayoutAttributeTop
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:_ClicnicTable
                                                                                 attribute:NSLayoutAttributeTop
                                                                                multiplier:1.0f
                                                                                  constant:width*20/640];
    NSLayoutConstraint *LeftConstrainforSearchButton=[NSLayoutConstraint constraintWithItem:_SearchButton
                                                                                 attribute:NSLayoutAttributeLeading
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:_SearchView
                                                                                 attribute:NSLayoutAttributeLeading
                                                                                multiplier:1.0f
                                                                                  constant:width*20/640];
    
    NSLayoutConstraint *HeightConstrainforSearchButton=[NSLayoutConstraint constraintWithItem:_SearchButton
                                                                                    attribute:NSLayoutAttributeHeight
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:nil
                                                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                                                   multiplier:1.0f
                                                                                     constant:width*50/640];
    NSLayoutConstraint *EqualConstrainforSearchButton=[NSLayoutConstraint constraintWithItem:_SearchButton
                                                                                    attribute:NSLayoutAttributeHeight
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:_SearchButton
                                                                                    attribute:NSLayoutAttributeWidth
                                                                                   multiplier:1.0f
                                                                                     constant:0];
                                                       
    TopConstrainforSearchButton.active=YES;
    LeftConstrainforSearchButton.active=YES;
    HeightConstrainforSearchButton.active=YES;
    EqualConstrainforSearchButton.active=YES;
    
    _SearchButton.backgroundColor=[UIColor whiteColor];
    _SearchButton.layer.cornerRadius=width*25/640;
    _SearchButton.layer.masksToBounds=YES;
    _SearchButton.layer.borderColor=[[UIColor whiteColor]CGColor];
    _SearchButton.layer.borderWidth= 1.0f;
    NSString *sr=@"🔍";
    NSRange ranger=[sr rangeOfString:sr];
    NSMutableAttributedString *SearchString=[[NSMutableAttributedString alloc]  initWithString:sr];
    [SearchString addAttribute:NSForegroundColorAttributeName
                         value:(id)[UIColor greenColor].CGColor range:ranger];
    
    [self.SearchButton setAttributedTitle:SearchString forState:UIControlStateNormal];
    self.tabBarController.tabBar.backgroundColor=[UIColor whiteColor];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)changcolor:(id)sender {
    UIButton *bb=(UIButton *)sender;
    bb.backgroundColor=[UIColor blueColor];
    
}

-(NSInteger) tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TableSampleIdentifier";
    ClinicTableViewCell *cell = (ClinicTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell=[[ClinicTableViewCell alloc] initWithFrame:CGRectMake(0, 0, self.SearchView.bounds.size.width, self.SearchView.bounds.size.width/4)];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return _SearchView.bounds.size.width*516/640;
}

//searchbar actions
- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar

{
    /*if (isSearching==NO) {
        self.ClicnicTable.hidden=YES;
        self.SearchEntityView.hidden=NO;
        isSearching=YES;
    }
    else{
        self.ClicnicTable.hidden=NO;
        self.SearchEntityView.hidden=YES;
        self.SearchButton.hidden=YES;
        isSearching=NO;
    }
   */
    
}

- (IBAction)ChangeToSearchView:(id)sender {
    [self performSegueWithIdentifier:@"search2" sender:self];
    isSearching=YES;
}

- (IBAction)ChangeToDetailView:(id)sender {
    [self performSegueWithIdentifier:@"Detail" sender:self];
}
     
    // - (IBAction)unwindSegue:(UIStoryboardSegue *)sender{
       //  NSLog(@"unwindSegue %@", sender);
    // }

- (void)ConfirmSearchContent:(NSString *)SearchContent{
    self.SearchContent=SearchContent;
   }

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"search2"]){
    self.cs=segue.destinationViewController;
    ((ClinicSearchViewController *)segue.destinationViewController).delegate=self;
    ((ClinicSearchViewController *)segue.destinationViewController).SearchContent=self.SearchContent;
    PushSegue *s = (PushSegue *)segue;
    s.isDismiss = NO;
    
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
        s.isLandscapeOrientation = YES;
    }
    else
    {
        s.isLandscapeOrientation = NO;
    }
    }
        else{
            self.detail=segue.destinationViewController;
        }
    
    if([segue.identifier isEqualToString:@"Detail"]){
        self.detail.firstController=self;
    }

}

@end
